package com.hjimi.ski.utils;

import com.hjimi.ski.bean.SyncImageListBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By 孙岩
 * on 2023/11/22
 */
public class ImageListUtil {
    public static List<SyncImageListBean.DataBean> getImageList(String imageListJson){
        List<SyncImageListBean.DataBean> list = new ArrayList<>();
        try {
            JSONObject obj = new JSONObject(imageListJson);
            if (obj.getInt("code") == 200) {
                JSONArray array = new JSONArray(obj.getString("data"));


                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = new JSONObject(array.get(i).toString());
                    JSONObject bgmConfigObj = new JSONObject(object.getString("bgm_config"));
                    SyncImageListBean.DataBean.BgmConfigBean bgmConfigBean = new SyncImageListBean.DataBean.BgmConfigBean(
                            JsonParseUtil.getBoolean(bgmConfigObj, "is_open"),
                            JsonParseUtil.getInt(bgmConfigObj, "bgm_id"),
                            JsonParseUtil.getString(bgmConfigObj, "bgm_url"),
                            JsonParseUtil.getBoolean(bgmConfigObj, "is_loop")
                    );
                    JSONObject cardConfigObj = new JSONObject(object.getString("card_config"));
                    SyncImageListBean.DataBean.CardConfigBean cardConfigBean = new SyncImageListBean.DataBean.CardConfigBean(
                            JsonParseUtil.getBoolean(cardConfigObj, "is_open"),
                            JsonParseUtil.getString(cardConfigObj, "title"),
                            JsonParseUtil.getString(cardConfigObj, "desc"),
                            JsonParseUtil.getString(cardConfigObj, "author"),
                            JsonParseUtil.getString(cardConfigObj, "period"),
                            JsonParseUtil.getBoolean(cardConfigObj, "is_narration"),
                            JsonParseUtil.getBoolean(cardConfigObj, "is_qr"),
                            JsonParseUtil.getString(cardConfigObj, "qr_url"),
                            JsonParseUtil.getInt(cardConfigObj, "carousel_seconds")
                    );
                    SyncImageListBean.DataBean bean = new SyncImageListBean.DataBean(
                            JsonParseUtil.getInt(object, "id"),
                            JsonParseUtil.getStr(object, "name"),
                            JsonParseUtil.getStr(object, "imageUrl"),
                            JsonParseUtil.getStr(object, "thumbUrl"),
                            JsonParseUtil.getStr(object, "qrUrl"),
                            JsonParseUtil.getInt(object, "artwork_id"),
                            bgmConfigBean,
                            cardConfigBean,
                            JsonParseUtil.getBoolean(object, "is_default")
                    );
                    list.add(bean);
                }
                return list;

            } else {
                ToastUtils.show(obj.getString("msg"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
